package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.info.PhotoInfo
import com.wt.struggle.goodbye.user.adapter.PhotoAdapter
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.my_photo_layout.*
import java.lang.StringBuilder

/**
 * 我的相册
 */
class MyPhotoActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {

            Send.MY_PHOTO_CODE -> {
                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {

                        val arr = gson!!.fromJson<ArrayList<PhotoInfo>>(
                            result,
                            object : TypeToken<ArrayList<PhotoInfo>>() {}.type
                        )

                        adapter.updateData(arr)

                    }
                }
            }

            Send.DELETE_PIC -> {
                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {
                    showToastShort("删除成功")

                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_photo_layout)

        tvTitle.text = "我的相册"
        tvRight.text = ""

        imageBack.setOnClickListener {
            finish()
        }


        tvRight.setOnClickListener {
            buttonDelete.visibility = View.VISIBLE
        }

        initAdapter()

        getMyPhoto()

        buttonDelete.setOnClickListener {

            adapter.change(true)

            delete()

        }
    }


    fun delete() {
        if (chooseAddMap.isNotEmpty()) {
            val sb = StringBuilder()
            for (temp in chooseAddMap) {
                val value = temp.value
                sb.append(value)
                sb.append(",")

            }

            val resultStr = sb.toString()
            val str = resultStr.subSequence(0, resultStr.length - 1).toString()
            send!!.deletePic(str, handler)
            showLoadDialog("删除中")


        } else {
            showToastShort("请选择图片")
        }

    }

    val chooseAddMap = hashMapOf<Int, Int>()

    fun addChoose(info: NewsInfo, b: Boolean) {
        if (b) {
            chooseAddMap[info.id!!] = info.id!!
        } else {
            chooseAddMap.remove(info.id!!)
        }

    }

    val list: ArrayList<PhotoInfo> = arrayListOf()
    lateinit var adapter: PhotoAdapter

    private fun initAdapter() {

        photoRecyclerView.layoutManager = LinearLayoutManager(this)
        adapter = PhotoAdapter(this, list)
        photoRecyclerView.adapter = adapter

    }

    var page = 1

    private fun getMyPhoto() {
        send!!.getMyPhotoList(page, handler)
        showLoadDialog("获取中")
    }

}